#include "drv_esp.h"
#include "drv_systick.h"
#include "drv_usart.h"
#include "stdio.h"
#include "string.h"
#include "drv_time.h"
#include "FreeRTOS.h"
#include "task.h" 

void drv_esp_init(void)
{
    vTaskDelay(2000);  // 等待模块启动

    // 1. 重置模组（可选，频繁重置可能影响稳定性）
    
    usart_sendstr(USART3, "AT+RESTORE\r\n", strlen("AT+RESTORE\r\n"));
    vTaskDelay(3000);  // 重置后需更长延时

    // 2. 测试AT指令
    usart_sendstr(USART3, "AT\r\n", strlen("AT\r\n"));
    vTaskDelay(500);

    // 3. 设置Station模式
    usart_sendstr(USART3, "AT+CWMODE=1\r\n", strlen("AT+CWMODE=1\r\n"));
    vTaskDelay(1000);

    // 4. 连接WiFi（密码正确的话，15秒足够）
    usart_sendstr(USART3, "AT+CWJAP_DEF=\"hqyjcs\",\"hqyj2022\"\r\n", strlen("AT+CWJAP_DEF=\"hqyjcs\",\"hqyj2022\"\r\n"));
    vTaskDelay(10000);

    // 5. 建立TCP连接
    usart_sendstr(USART3, "AT+CIPSTART=\"TCP\",\"192.168.91.104\",8080\r\n", strlen("AT+CIPSTART=\"TCP\",\"192.168.50.100\",8080\r\n"));
//    usart_sendstr(USART3, "AT+CIPSTART=\"TCP\",\"192.168.50.100\",2025\r\n", strlen("AT+CIPSTART=\"TCP\",\"192.168.50.100\",2025\r\n"));
    vTaskDelay(2000);  // 等待连接建立

    // 6. 发送数据（关键步骤：去除延时，立即发送）
    usart_sendstr(USART3, "AT+CIPSEND=13\r\n", strlen("AT+CIPSEND=13\r\n"));  // 明确指定实际长度（13字节）
    vTaskDelay(100);  // 极短延时，等待模块返回>提示符（不可太长）

    // 7. 立即发送数据（必须在>出现后发送）
    usart_sendstr(USART3, "Client Ready\r\n", strlen("Client Ready\r\n"));  // 修正拼写错误：Clinet→Client
    vTaskDelay(1000);  // 等待发送完成
}
void usart_sendstr(USART_TypeDef* USARTx, char * str,int size)
{
    uint8_t i =0;
    USART_ClearFlag(USART3,USART_FLAG_TC);
    for(i=0;i<size;i++)
    {
        USART_SendData(USART3,*str);
        while(RESET == USART_GetFlagStatus(USART3, USART_FLAG_TC));
        USART_ClearFlag(USART3,USART_FLAG_TC);
        str++;
    }    
}
extern int back_state; // 0:停止, 1:行走中
extern int back_step;
extern uint8_t usart3_rx_len;
extern uint8_t usart3_rx_buf[128];
extern uint8_t usart3_data_ready_flag;
extern uint8_t usart3_rx_overflow;
extern volatile int walk_state; 
extern volatile int walk_step; 
extern void process_control_command();
#define USART3_RX_BUF_LEN 128
void usart3_command(void)
{
    uint8_t i = 0;
    uint8_t count = 0;
    uint8_t cmd_len = 0;
    printf("init_cmd=%s",usart3_rx_buf);
    usart_sendstr(USART3,"ledon",strlen("ledon"));
    //查找：位置
    for(i=0;i<usart3_rx_len;i++)
    {
        if(usart3_rx_buf[i] != ':')
        {
            process_control_command();
        }
        if(usart3_rx_buf[i] == ':')
        {
            break;
        }                  
        count++;
    }
    //获取：之后的数据
    cmd_len = usart3_rx_len - count -1;
    for(i=0;i<cmd_len;i++)
    {
        usart3_rx_buf[i] = usart3_rx_buf[count+1+i];
    }
    usart3_rx_buf[cmd_len] = '\0';
    
    printf("finish_cmd=%s",usart3_rx_buf);
    /*if(!usart3_data_ready_flag)
    {
        return ;
    }*/

    if(strstr((char*)usart3_rx_buf,"ledon") != NULL)
    {
        drv_led_open();
    }
    else if(strstr((char*)usart3_rx_buf,"ledoff") != NULL)
    {
        drv_led_close();
    }
    else if(strstr((char*)usart3_rx_buf,"fanon") != NULL)
    {
        GPIO_SetBits(GPIOC, GPIO_Pin_6);
    }
	else if(strstr((char*)usart3_rx_buf,"fanoff") != NULL)
    {
        GPIO_ResetBits(GPIOC, GPIO_Pin_6);
    }
    else if(strstr((char*)usart3_rx_buf,"walk") != NULL)
    {
        walk_state = 1;
        walk_step = 0;
        back_state = 0;
 
    }
    else if(strstr((char*)usart3_rx_buf,"back") != NULL)
    {    
        walk_state = 0;
        back_state = 1; // 0:停止, 1:行走中
        back_step = 0;
        //drv_tim3_back(1); //后退       
    }
    else if(strstr((char*)usart3_rx_buf,"standup") != NULL)
    {       
        walk_state = 0;
        back_state = 0;
        drv_tim3_stand();//站立
    }
    else if(strstr((char*)usart3_rx_buf,"sit") != NULL)
    {
        walk_state = 0;
        back_state = 0;
        drv_tim3_down();//坐下               
    }    
    
    usart3_data_ready_flag = 0;
    usart3_rx_len = 0;
    memset(usart3_rx_buf,0,USART3_RX_BUF_LEN);
    usart3_rx_overflow = 0;
}


